package engine.logic.loading
{
	import engine.control.loading.NxLoading2;
	import engine.data.ResPath;
	import engine.scene.SceneChangeManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	
	import newx.StageInfo;
	import newx.com.loader.SwfLoader;
	import newx.utils.debug.IO;

	public class LoadingLogic
	{
		static private var _self:LoadingLogic;
		
		private var _nxLoading:NxLoading2;
		
		private var _forceVisible:Boolean	=	true;
		
		public function get nxLoading():NxLoading2
		{
			return _nxLoading;
		}

		public static var _loadedCallBack:Function;
		
		public function LoadingLogic()
		{
			if(_self)	throw new Error("LoadingLogic 单例");
			var bar:DisplayObject = StageInfo.loadingbar;
//			if(bar)
//			{
//				resLoaded(StageInfo.loadingbar as MovieClip,null);
//			}else
//			{
				SwfLoader.StartLoad(ResPath.SWF + "loading.swf",null,resLoaded);
//			}
		}
		
		static public function getInstance():LoadingLogic
		{
			if(_self==null)	_self=new LoadingLogic();
			
			return _self;
		}
		
		private function resLoaded(val:MovieClip,pList:Array):void
		{
			if(val==null)	return;
			
			_nxLoading=new NxLoading2(val);
			
			_nxLoading.shieldArea(SceneChangeManager.getGameWidth(),SceneChangeManager.getGameHeight());
			
			onChangeScene();
			
			if(_loadedCallBack!=null)	_loadedCallBack();
		}
		
		public function onChangeScene():void
		{
			_nxLoading.onChangeScene(Math.min(StageInfo.rootMaxW, SceneChangeManager.getGameWidth()),
									Math.min(StageInfo.rootMaxH, SceneChangeManager.getGameHeight()),
									SceneChangeManager.getGameX(),
									SceneChangeManager.getGameY());
		}
		public function setProcess(proc:int,titleStr:String=""):void
		{
			if(proc == 100)
			{
				IO.traceLog("场景进度条100%关闭");
				if(_nxLoading)	
				{
					IO.traceLog("场景进度条存在关闭end");
					_nxLoading.visible = false;
					_nxLoading.setProcess(proc,titleStr);	
				}
				return;
			}else
			{
				_nxLoading.visible = true;
			}
			if(_forceVisible)
			{
				//			trace(titleStr+flash.utils.getTimer()+"loaded..........:"+proc);
				if(_nxLoading)	
					_nxLoading.setProcess(proc,titleStr);	
			}
			else
			{
				if(_nxLoading)
					_nxLoading.visible=_forceVisible;
			}
		}
		
		public function hide():void
		{
			if(_nxLoading)	_nxLoading.hide();
		}
		
		public function set forceVisible(val:Boolean):void
		{
			_forceVisible=val;
			
			if(_nxLoading)
				_nxLoading.visible=_forceVisible;
			
		}
		
	}
}